package com.hk.yajin.function.members.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.hk.developmentlib.baseben.NewWorkBean;
import com.hk.developmentlib.callback.HttpCallBack;
import com.hk.developmentlib.utills.FastjsonUtils;
import com.hk.yajin.R;
import com.hk.yajin.base.BaseActivity;
import com.hk.yajin.model.RegArea;
import com.hk.yajin.utills.HttpRequestUtils;
import com.hk.yajin.utills.url.UrlConstants;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AcountDistrictListActivity extends BaseActivity {

    @BindView(R.id.rcy_list)
    RecyclerView rcyList;

    @BindView(R.id.activity_acount_district_list)
    LinearLayout activityAcountDistrictList;

    public static final String DISTRICT_NUM = "num";
    public static final String DISTRICT_NAME = "name";
    List<RegArea> datas = new ArrayList<>();
    BaseQuickAdapter<RegArea, BaseViewHolder> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_acount_district_list);
        ButterKnife.bind(this);
        initToolBar(R.string.country_district);
        initView();
        initData();
    }

    private void initView() {
        adapter = new BaseQuickAdapter<RegArea, BaseViewHolder>(R.layout.list_item_reg_area, datas) {
            @Override
            protected void convert(BaseViewHolder helper, RegArea item) {
                helper.setText(R.id.tv_name, item.getArea_name());
                helper.setText(R.id.tv_code, item.getArea_code());
            }
        };
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                setResult(RESULT_OK, new Intent().putExtra(DISTRICT_NUM, datas.get(position).getArea_code()).putExtra(DISTRICT_NAME, datas.get(position).getArea_name()));
                finish();
            }
        });
        rcyList.setLayoutManager(new LinearLayoutManager(mContext));
        rcyList.setAdapter(adapter);
    }

    private void initData() {
        showLoadingView();
        Map<String, Object> reqParam = new HashMap<String, Object>();
        HttpRequestUtils.get(true, UrlConstants.FUNCTION_GET_AREA_CONFIG, reqParam, false, new HttpCallBack() {

            @Override
            public void onSucced(String data) {
                hideLoadingView();
                setData(data);
            }

            @Override
            public void onErrorCode(NewWorkBean bean) {
                hideLoadingView();
                showShortToast(bean.getMsg());
            }

            @Override
            public void onBadHttp(String tip) {
                hideLoadingView();
                showShortToast(tip);
            }

        });
    }
//
//    @OnClick({ })
//    public void onViewClicked(View view) {
//        switch (view.getId()) {
//
//        }
//    }


    private void setData(String data) {
        if (!TextUtils.isEmpty(data) && data.contains("reg_areas")) {
            String regareaString = FastjsonUtils.getJsonString(data, "reg_areas");
            List<RegArea> temData = FastjsonUtils.getBeanList(regareaString, RegArea.class);
            if (temData != null && temData.size() > 0) {
                datas.clear();
                datas.addAll(temData);
                adapter.setNewData(datas);
                adapter.notifyDataSetChanged();
            }
        }

    }
}
